
exports.upgrade = function(player, req, resp, onHandled) {
    do {
        if( isNaN(req.args.eid) || isNaN(req.args.slot) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        var eid = +req.args.eid;
        var slot = Math.round(req.args.slot);

        var user = player.user;

        var equip = user.equip[eid];
        if( !equip ) {
            resp.code = 1; resp.desc = 'no equip'; break;
        }
        
        var gemNeed = 0;
        if( !equip.gems[slot] ) {
            // 没有宝石,需要激活,需要判断激活等级
            if( equip.strength < gConfGlobal['EquipOpenGemStrength'+slot] ) {
                resp.code = 1; resp.desc = 'strength not enough'; break;
            }
            var defaultGem = gConfGlobal[equip.type.capWord() + "GemSlot"][slot-1];
            if( !defaultGem ) {
                resp.code = 1; resp.desc = 'invalid slot'; break;
            }
            gemNeed = defaultGem;
            player.doTask('OpenGem','all');
        }else{
            gemNeed = equip.gems[slot]+1; 
            if( !gConfGem[gemNeed] ) {
                resp.code = 1; resp.desc = 'up to max'; break;
            }

            if( !player.addInventory('gem', gemNeed, -1) ) {
                resp.code = 1; resp.desc = 'no more gem'; break;
            }

            resp.data.gem = gemNeed;
        }
        
        equip.gems[slot] = gemNeed;
        player.markDirty(util.format('equip.%d.gems.%d', eid, slot));
        player.roleDirty = true;
    }while(false);

    onHandled();
}

exports.merge = function(player, req, resp, onHandled) {
    do {
        if( isNaN(req.args.gid) || isNaN(req.args.count) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        var gid = +req.args.gid;
        var count = Math.round(req.args.count);

        var user = player.user;

        if( count < 1 ) {
            resp.code = 1; resp.desc = 'invalid count'; break;
        }

        var newGid = gid+1;
        var newGemConf = gConfGem[newGid];
        if( !gConfGem[gid] || !newGemConf) {
            resp.code = 1; resp.desc = 'invalid gid'; break;
        }

        var gemOpenLimit = gConfGlobal['GemLevelOpenDays'+newGid];
        if( gemOpenLimit ) {
            var openTime = gConfGlobalServer.ServerStartTime + gemOpenLimit*86400;
            if( common.getTime() < openTime ) {
                resp.code = 1; resp.desc = 'not open yet'; break;
            }
        }

        if( !player.addInventory('gem', gid, -3*count) ) {
            resp.code = 1; resp.desc = 'not enough gem'; break;
        }

        player.addInventory('gem', newGid, count);

        if( newGid%100 >= 10 ) {
            gMessage.addSystemMessage('merge_gem', [user.info.un, newGemConf.Name]);
        }

        resp.data.gem = gid+1;
        
        player.checkEvent('DoMergeGem', newGemConf.Quantity);
    }while(false);

    onHandled();
}
